package com.cn.tous.auth.service;

import com.cn.tous.auth.dtos.AuthUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author mengwei
 * @description UserDetailsServiceImpl
 * @createDate 2025/7/26 14:12
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库查询用户


        // 检查用户状态
//        if (user.getStatus() != 1) {
//            throw new UsernameNotFoundException("用户已被禁用: " + username);
//        }

        // 转换角色权限
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        AuthUser user = new AuthUser(username, "123456", authorities);
        user.setUsername(username);
        user.setPassword("123456");
        user.setId(11L);
        user.setNickname("dream");
        user.setRoles(new HashSet<>());
        user.setStatus(1);
        user.getRoles().add("ROLE_ADMIN");
        user.getRoles().add("ROLE_USER");

        // 构建并返回UserDetails对象
        return user;
    }
}
